﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FacebookController
{
	#if UNITY_IPHONE || UNITY_ANDROID
	
	List<FacebookFriend> myFriendsList;

	List<FacebookAppFriend> myAppFriendsList;

	public FacebookMeResult _Me;

	
	public delegate void Action();
	// Fired when a graph request finishes
	public event Action graphRequestCompletedEvent;
	
	// Fired when a graph request fails
	public event Action graphRequestFailedEvent;

	public bool IsLogged()
	{		
		return FacebookBinding.isSessionValid ();
	}
	public void LogIn ()
	{	
		Debug.Log("Enter Log in");
		if (!FacebookBinding.isSessionValid ())
		{
			Debug.Log("Loging in");
			FacebookBinding.login ();
		}
	}
	public void LogOut ()
	{			
		if (FacebookBinding.isSessionValid ())
			FacebookBinding.logout ();
	}
	// Use this for initialization
	public FacebookController ()
	{
		FacebookBinding.init ();
		myFriendsList = new List<FacebookFriend>();
		myAppFriendsList = new List<FacebookAppFriend>();
	}	

	public void CleanGraphActions()
	{
		graphRequestCompletedEvent = null;
		graphRequestFailedEvent = null;
	}	

	public void InvitePlayer() {

		Debug.Log("INVITE PLAYERS");
		string title = "Invitar amigos a jugar AnswerMe!";
		string message = "Juega una partida en AnswerMe!";
		
		Dictionary<string, string> lParam = new Dictionary<string, string>();
		lParam["app_id"] = "659121550819566";
		lParam["message"] = message;
		lParam["title"] = title;
		FacebookBinding.showDialog("apprequests", lParam);
	}

	public void Post(string message)
	{
		if (FacebookBinding.isSessionValid ()) {
			Facebook.instance.postMessage((message),((string arg1, object arg2) => postHandler(arg1, arg2)));
		}
	}

	void postHandler( string error, object result )
	{
		if( error != null )
		{
			Debug.Log(error );
			if (graphRequestFailedEvent != null)
				graphRequestFailedEvent();
		}
		else
		{
			if (graphRequestCompletedEvent != null)
				graphRequestCompletedEvent();
		}
	}
	
	void getFriendsHandler( string error, FacebookFriendsResult result )
	{
		if( error != null )
			Debug.Log(error );
		else
		{
			foreach (FacebookFriend fbf in result.data)
			{
				myFriendsList.Add(fbf);
			}
		}
	}    
	
	void getFriendsAppHandler( string error, FacebookAppFriendsResult result )
	{
		if( error != null )
		{
			Debug.Log(error );
			if (graphRequestFailedEvent != null)
				graphRequestFailedEvent();
		}
		else
		{
			foreach (FacebookAppFriend fbf in result.data)
			{
				if (fbf.installed)
				{
					myAppFriendsList.Add(fbf);
				}
			}
			if (graphRequestCompletedEvent != null)
				graphRequestCompletedEvent();
		}
	}

	void getMeHandler( string error, FacebookMeResult result )
	{
		if( error != null )
		{
			Debug.Log("GET ME ERROR: " + error );
			if (graphRequestFailedEvent != null)
				graphRequestFailedEvent();
		}
		else
		{
			Debug.Log("GET ME RESULT: " + result.ToString() );
			_Me = result;
			if (graphRequestCompletedEvent != null)
				graphRequestCompletedEvent();
		}
	}
	
	public void GetFacebookFriends()
	{
		//Facebook.instance.getFriends( (string arg1, FacebookFriendsResult arg2) => getFriendsHandler(arg1, arg2));
		Facebook.instance.getAppFriends( (string arg1, FacebookAppFriendsResult arg2) => getFriendsAppHandler(arg1, arg2));
	}
	
	public void GetMe()
	{
		Facebook.instance.getMe ((string arg1, FacebookMeResult arg2) => getMeHandler(arg1, arg2));
	}

	public void RemoveInvalidFriend(string id)
	{
		FacebookAppFriend invalid = null;
		foreach (FacebookAppFriend friend in myAppFriendsList)
			if (friend.id == id)
				invalid = friend;

		if(invalid != null)
			myAppFriendsList.Remove (invalid);
	}

	/*public FacebookAppFriend GetCurrentFriend()
	{
		return myAppFriendsList[appFriendID];
	}
	public FacebookAppFriend GetNextFriend()
	{
		if (myAppFriendsList.Count > 0)
		{
			appFriendID ++;
			if (appFriendID == myAppFriendsList.Count)
				appFriendID = 0;

			return myAppFriendsList[appFriendID];
		}
		return null;
	}

	public FacebookAppFriend GetPrevFriend()
	{
		if (myAppFriendsList.Count > 0)
		{
			appFriendID --;
			if (appFriendID == (-1))
				appFriendID = (myAppFriendsList.Count-1);
			
			return myAppFriendsList[appFriendID];
		}
		return null;
	}*/

	public int GetFriendsCount()
	{
		if (myAppFriendsList.Count >= 0)
		{
			return myAppFriendsList.Count;
		}
		return 0;
	}
	public void ClearFriends()
	{
		if (myAppFriendsList != null)
		{
			myAppFriendsList.Clear ();
		}
	}
	#endif
}